package task

import (
	"time"
	proto "xagent/proto/v1"
)

// TaskState represents the current state of a task
type TaskState struct {
	Status    proto.TaskStatus
	Progress  float64
	StartTime time.Time
	EndTime   time.Time
	Retries   int
	Error     error
}

// TaskHistoryEntry represents a historical event in task execution
type TaskHistoryEntry struct {
	Timestamp time.Time
	Event     string
	Status    proto.TaskStatus
	Message   string
	Error     error
}

// Task represents a task in the system
type Task struct {
	ID                string
	Name              string
	Description       string
	Status            proto.TaskStatus
	Priority          int
	CreatedAt         time.Time
	StartedAt         *time.Time
	CompletedAt       *time.Time
	Dependencies      []string
	RequiredResources map[string]float64
	AssignedTo        string
	Result            interface{}
	Error             error
	RetryCount        int
	Metadata          *proto.TaskMetadata
}

// TaskResult represents the result of a task execution
type TaskResult struct {
	Error  string
	Output string
	Data   map[string]interface{}
}

// NewTaskResult creates a new task result
func NewTaskResult() *TaskResult {
	return &TaskResult{
		Data: make(map[string]interface{}),
	}
}

// SetError sets the error message in the task result
func (tr *TaskResult) SetError(err error) {
	if err != nil {
		tr.Error = err.Error()
	}
}

// SetOutput sets the output message in the task result
func (tr *TaskResult) SetOutput(output string) {
	tr.Output = output
}

// AddData adds a key-value pair to the task result data
func (tr *TaskResult) AddData(key string, value interface{}) {
	tr.Data[key] = value
}

// NewTask creates a new task instance
func NewTask(id, name string) *Task {
	return &Task{
		ID:                id,
		Name:              name,
		Status:            proto.TaskStatus_TASK_STATUS_PENDING,
		CreatedAt:         time.Now(),
		RequiredResources: make(map[string]float64),
		Metadata: &proto.TaskMetadata{
			Metadata:          make(map[string]string),
			RequiredResources: make(map[string]float64),
		},
	}
}
